﻿/*
 * By David Barrett, Microsoft Ltd. 2012. Use at your own risk.  No warranties are given.
 * 
 * DISCLAIMER:
 * THIS CODE IS SAMPLE CODE. THESE SAMPLES ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND.
 * MICROSOFT FURTHER DISCLAIMS ALL IMPLIED WARRANTIES INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR OF FITNESS FOR
 * A PARTICULAR PURPOSE. THE ENTIRE RISK ARISING OUT OF THE USE OR PERFORMANCE OF THE SAMPLES REMAINS WITH YOU. IN NO EVENT SHALL
 * MICROSOFT OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS,
 * BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING OUT OF THE USE OF OR INABILITY TO USE THE
 * SAMPLES, EVEN IF MICROSOFT HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. BECAUSE SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION
 * OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES, THE ABOVE LIMITATION MAY NOT APPLY TO YOU.
 * */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.IO;

namespace SOAP_Test_Application.EWSTools
{
    public partial class FormEWSSetImpersonation : Form
    {
        private IWin32Window oParentWindow = null;

        public FormEWSSetImpersonation(IWin32Window Parent)
        {
            InitializeComponent();
            oParentWindow = Parent;
        }

        public void GetImpersonationHeader(TextBox ImpersonationTextbox)
        {
            textBoxID.Text = ImpersonationTextbox.Text;
            this.ShowDialog(oParentWindow);
            string sHeader = ReadTemplate();
            string sIDType = "";
            if (comboBoxMethod.SelectedIndex < 0) return;


            switch (comboBoxMethod.Text.Substring(0, 1).ToLower())
            {
                case "p":
                    // Primary SMTP Address
                    sIDType = "PrimarySmtpAddress";
                    break;

                case "u":
                    // User Principal Name
                    sIDType = "PrincipalName";
                    break;

                case "s":
                    // SID
                    sIDType = "SID";
                    break;
            }
            if (String.IsNullOrEmpty(sIDType)) return;
            ImpersonationTextbox.Tag = sIDType;
            ImpersonationTextbox.Text = textBoxID.Text;
            //sHeader = sHeader.Replace("%IMPERSONATIONID%", "<t:" + sIDType + ">" + textBoxID.Text + "</t:" + sIDType + ">");
            return;
        }


        private string ReadTemplate()
        {
            // Reads the XML template
            Assembly oAssembly = Assembly.GetExecutingAssembly();
            StreamReader oReader = new StreamReader(oAssembly.GetManifestResourceStream("SOAP_Test_Application.EWSTools.EWSImpersonationHeader.xml"));
            string sTemplate = oReader.ReadToEnd();
            oReader.Close();
            return sTemplate;
        }

        private void buttonApply_Click(object sender, EventArgs e)
        {
            this.Hide();
        }
    }
}
